// lib/comments.js
import {db} from './db'

export async function createComment({slug, userId, message}) {
    return await db.comment.create({
        data: {slug, userId, message}
    })
}

export async function getComments(slug) {
    // 模拟延迟
    // await new Promise((resolve) => setTimeout(resolve, 3000))
    return await db.comment.findMany({
        where: {slug},
        orderBy: {postedAt: 'desc'},
        include: {
            // 包含外表数据
            // user: true
            user: {
                // 优化: 只查询name列的数据
                select: {name: true}
            }
        }
    })
}